<?php

class Social_Controller_Browse_Twitter_Oauth2callback
{

	function execute($request)
	{
		$adapter = Nuo::getObject('social/adapter/twitter');

		$clientApi = $adapter -> getClientApi();
		
		$aParams = $_REQUEST;
		
		if (isset($aParams['oauth_token']) && $aParams['oauth_token'] && isset($aParams['oauth_verifier']) && $aParams['oauth_verifier'])
		{
			$oauth_token = $aParams['oauth_token'];
			$oauth_verifier = $aParams['oauth_verifier'];

			$response = $clientApi -> oAuthAccessToken($oauth_token, $oauth_verifier);
			
			var_dump($response);

			if (isset($response['oauth_token']))
			{
				$clientApi -> setOAuthToken($response['oauth_token']);
				$clientApi -> setOAuthTokenSecret($response['oauth_token_secret']);
				$me = $clientApi -> accountVerifyCredentials();

				$aProfile = array();
				$aProfile['user_name'] = isset($me['screen_name']) ? $me['screen_name'] : "";
				$aProfile['full_name'] = isset($me['name']) ? $me['name'] : "";
				$aProfile['id'] = isset($me['id_str']) ? $me['id_str'] : 0;
				$aProfile['service'] = 'twitter';
				$aProfile['img_url'] = isset($me['profile_image_url']) ? $me['profile_image_url'] : "";
				$aProfile['followers_count'] = isset($me['followers_count']) ? $me['followers_count'] : 0;
				
				var_dump($aProfile);
			}
			else
			{
				echo 'failed';
				exit;
			}

		}
		else
		{
			$clientApi -> oAuthRequestToken($callbackUrl);
			$clientApi -> oAuthAuthorize();
		}

	}

}
